import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-10
 * Time: 8:21
 * author:MyStack
 */
public class MyStack {
    private int [] s;
    private int size;
    private int max = 2;
    public MyStack () {
        s = new int[max];
        size = 0;
    }

    //增加
    public void push(int val) {
        //判断是否满了
        if( isfull()) {
            s = Arrays.copyOf(s , 2*max);
            max*=2;
        }
        s[size] = val;
        size++;
    }

    //弹出栈顶元素
    public int pop() {
        //判断栈为空
        if( isempty()) {
            System.out.println("栈为空");
            return 0;
        }
        //返回栈顶元素
        int ret = s[size-1];
        size--;
        return ret;
    }

    //获取栈顶元素
    public int peek() {
        //判断栈为空
        if( isempty()) {
            System.out.println("栈为空");
            return 0;
        }
        return s[size-1];
    }
    //
    private boolean isempty() {
        return size == 0;
    }
    private boolean isfull() {
        return size == max;
    }
}
